<template>
  <el-container class="home_container">
    <!-- 头部区域-->
    <el-header>
      <div>
        <img src="../assets/bdqn.png" alt="">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>

    <el-container>
      <!-- 侧边菜单栏区域-->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!--可折叠按钮-->
        <div class="toggle_button" @click="toggleCollapse">|||</div>
        <!--菜单栏区域-->
        <el-menu
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#ff00cc"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!--一级菜单-->
          <el-submenu :index="'/' + item.path" v-for="item in menuList" :key="item.id">
            <!--一级菜单模板区域  -->
            <template slot="title">
              <!--图标-->
              <i :class="iconObj[item.id]"></i>
              <!--内容-->
              <span>{{ item.authName }}</span>
            </template>

            <!--二级菜单-->
            <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveNavPath('/' + subItem.path)">
              <!--二级菜单模板区域   -->
              <template slot="title">
                <!--图标-->
                <i class="el-icon-menu"></i>
                <!--内容-->
                <span>{{ subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
       </el-aside>
      <!-- 内容主体区域-->
      <el-main>
      <!--路由占位符-->
      <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
    export default {
      data (){
        return{
          //所有菜单列表数据
          menuList:[],
        //  一级菜单字体图标对象
          iconObj:{
            '125':'iconfont icon-user',
            '103':'iconfont icon-tijikongjian',
            '101':'iconfont icon-shangpin',
            '102':'iconfont icon-danju',
            '145':'iconfont icon-baobiao',
          },
          //控制菜单左侧的折叠
          isCollapse:false,
          //默认激活的导航菜单路径
          activePath:''
        }
      },
      created() {
        this.getMenuList()
        //读取sessionStorage中保存的路径
        this.activePath = window.sessionStorage.getItem('activePath')
      },
      methods:{
        //点击退出按钮，退出登录
        logout(){
          //清空sessionStorage内保存的token
          window.sessionStorage.clear()
          //通过编程式路由导航跳转到login页面
          this.$router.push('/login')
        },
        //获取左侧菜单列表数据
        async getMenuList(){
          //发送请求
          const { data:res } = await this.$http.get('menus')
          //判断请求是否成功
          if(res.meta.status !== 200) return this.$message.error('获取菜单列表数据失败！')

          //将返回的数据保存到data
          this.menuList = res.data
        },
        //点击按钮，切换菜单的折叠与显示
        toggleCollapse(){
          this.isCollapse = !this.isCollapse
        },
        //点击保存二级菜单路径
        saveNavPath(activePath){
          //将路径保存在sessionStorage中
          window.sessionStorage.setItem('activePath',activePath)
          this.activePath = activePath
        }
      }
    }
</script>

<style lang="less">
  .home_container{
    height:100%;
  }
  .el-header{
    /*线性渐变*/
    background: linear-gradient(to right,#FFFFFF,#6DD5FA,#2980B9);
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: #6441A5;
    font-size: 20px;

    > div {
      display: flex;
      align-items: center;

      span{
        margin-left:15px ;
      }
    }
  }
  .el-aside{
    background: linear-gradient(to bottom,#536976,#292E49);
    .el-menu{
      border-right: 0;
    }
  }
  .el-main{
    background: linear-gradient(to bottom right,#bdc3c7,#2c3e50);
  }
  .iconfont{
    margin-right: 10px;
  }
  .toggle_button{
    background-color: #4a5064;
    font-size: 10px;
    line-height: 24px;
    text-align: center;
    color: #ffffff;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
</style>
